﻿using EOM.TSHotelManagement.Shared;
using Microsoft.Extensions.Configuration;
using SqlSugar;

namespace EOM.TSHotelManagement.EntityFramework
{
    public class SqlSugarClientConnector : ISqlSugarClientConnector
    {
        private readonly IConfiguration _configuration;

        public SqlSugarClientConnector(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public ISqlSugarClient CreateClient(string dbName = null)
        {
            // 读取默认数据库名称
            dbName ??= _configuration[SystemConstants.DefaultDatabase];
            string connectionString;
            if (Environment.GetEnvironmentVariable(SystemConstants.DockerEnv) != null)
            {
                connectionString = Environment.GetEnvironmentVariable(SystemConstants.DefaultDatabase) switch
                {
                    SystemConstants.PgSql => Environment.GetEnvironmentVariable($"{SystemConstants.PgSql}ConnectStr"), //Test passed
                    SystemConstants.MySql => Environment.GetEnvironmentVariable($"{SystemConstants.MySql}ConnectStr"), //Test passed
                    SystemConstants.SqlServer => Environment.GetEnvironmentVariable($"{SystemConstants.SqlServer}ConnectStr"), //Test passed
                    SystemConstants.Oracle => Environment.GetEnvironmentVariable($"{SystemConstants.Oracle}ConnectStr"), //Please manually test
                    SystemConstants.MariaDB => Environment.GetEnvironmentVariable($"{SystemConstants.MariaDB}ConnectStr"), //Test passed
                    _ => throw new ArgumentException("Unsupported database", nameof(dbName)),
                };
            }
            else
            {
                connectionString = _configuration.GetConnectionString(dbName + "ConnectStr");
            }

            var dbType = GetDbType(dbName);

            ConnectionConfig config = new()
            {
                ConnectionString = connectionString,
                DbType = dbType,
                InitKeyType = InitKeyType.Attribute,
                IsAutoCloseConnection = true,
                LanguageType = LanguageType.Chinese
            };

            switch (dbType)
            {
                case DbType.PostgreSQL:
                    config.MoreSettings = new ConnMoreSettings
                    {
                        PgSqlIsAutoToLower = true,
                        PgSqlIsAutoToLowerCodeFirst = true,
                        DisableMillisecond = true,
                    };
                    break;
                case DbType.SqlServer:
                    config.MoreSettings = new ConnMoreSettings()
                    {
                        SqlServerCodeFirstNvarchar = true,
                        IsWithNoLockQuery = true
                    };
                    break;
            }

            return new SqlSugarClient(config);
        }

        public static DbType GetDbType(string dbName)
        {
            return dbName switch
            {
                SystemConstants.PgSql => DbType.PostgreSQL,
                SystemConstants.MySql => DbType.MySql,
                SystemConstants.Oracle => DbType.Oracle,
                SystemConstants.SqlServer => DbType.SqlServer,
                SystemConstants.MariaDB => DbType.MySqlConnector,
                _ => throw new ArgumentException("Unsupported database", nameof(dbName))
            };
        }
    }
}
